package com.huang.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.huang.annotation.OptLogger;
import com.huang.model.PageResult;
import com.huang.model.Result;
import com.huang.model.request.LogQuery;
import com.huang.model.response.ExceptionLogResp;
import com.huang.model.response.OperationLogResp;
import com.huang.service.ExceptionLogService;
import com.huang.service.OperationLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.huang.constants.OptTypeConstant.DELETE;
import static com.huang.constants.OptTypeConstant.QUERY;

/**
 * 日志控制器
 *
 * @author Ikaros
 * @since 2025/9/5 21:05 星期五
 */
@RestController
@RequiredArgsConstructor
public class LogController {

    private static final String TAG = "日志模块";

    private final ExceptionLogService exceptionLogService;

    private final OperationLogService operationLogService;

    /**
     * 查看操作日志
     *
     * @param logQuery 条件
     * @return {@link OperationLogResp} 操作日志
     */
    @SaCheckPermission("log:operation:list")
    @GetMapping("/admin/operation/list")
    public Result<PageResult<OperationLogResp>> listOperationLogVO(LogQuery logQuery) {
        return Result.success("查看操作日志列表成功!", operationLogService.listOperationLogVO(logQuery));
    }

    /**
     * 删除操作日志
     *
     * @param logIdList 日志id集合
     * @return {@link Result<>}
     */
    @SaCheckPermission("log:operation:delete")
    @DeleteMapping("/admin/operation/delete")
    public Result<?> deleteOperationLog(@RequestBody List<Integer> logIdList) {
        operationLogService.removeByIds(logIdList);
        return Result.success("删除成功!");
    }

    /**
     * 删除异常日志
     *
     * @param logIdList 日志id集合
     * @return {@link Result<>}
     */
    @OptLogger(value = DELETE, tag = TAG, description = "删除异常日志")
    @SaCheckPermission("log:exception:delete")
    @DeleteMapping("/admin/exception/delete")
    public Result<?> deleteExceptionLog(@RequestBody List<Integer> logIdList) {
        exceptionLogService.removeByIds(logIdList);
        return Result.success("删除成功!");
    }


    /**
     * 查看异常日志
     *
     * @param logQuery 异常日志查询条件
     * @return {@link Result<ExceptionLogResp>} 异常日志列表
     */
    @OptLogger(value = QUERY, tag = TAG, description = "查看异常日志列表")
    @SaCheckPermission("log:exception:list")
    @GetMapping("/admin/exception/list")
    public Result<PageResult<ExceptionLogResp>> listExceptionLog(LogQuery logQuery) {
        return Result.success("查询成功!", exceptionLogService.listExceptionLog(logQuery));
    }
}
